/*
nil

资料
https://gfw.go101.org/article/nil.html
*/
package main

import (
	"fmt"
	"unsafe"
)

type People interface{}

type Student struct{}

func main() {
	var s *Student = nil
	fmt.Println(s == nil) // true

	var p People = nil
	fmt.Println(p == nil) // true

	// 接口类型值是“nil”：当且仅当动态值和动态类型同时为“nil”
	var p2 People = s
	fmt.Println(p2 == nil) // false

	// *main.Student <nil> *main.Student
	fmt.Printf("%T %T %T \n", s, p, p2)

	//var stu Student = nil // 编译出错，结构体默认不为nil，也不能跟nil比较

	var arr []int = nil
	fmt.Println(arr, arr == nil) // [] true
	arr = append(arr, 1)
	fmt.Println(arr) // [1]

	var m map[string]string = nil
	fmt.Println(m == nil)
	//m["1"] = "111" // 不可直接使用，需要make后才能使用

	nilNotKeyword()

	sizeOfNil()
}

func nilNotKeyword() {
	nil := 123
	fmt.Println(nil) // 123
}

func sizeOfNil() {
	fmt.Println("=====sizeOfNil=====")

	var p *struct{} = nil
	fmt.Println(unsafe.Sizeof(p)) // 8

	var s []int = nil
	fmt.Println(unsafe.Sizeof(s)) // 24

	var m map[int]bool = nil
	fmt.Println(unsafe.Sizeof(m)) // 8

	var c chan string = nil
	fmt.Println(unsafe.Sizeof(c)) // 8

	var f func() = nil
	fmt.Println(unsafe.Sizeof(f)) // 8

	var i interface{} = nil
	fmt.Println(unsafe.Sizeof(i)) // 16
}
